package org.fatmansoft.teach.util;

import com.alibaba.fastjson.JSONObject;
import org.fatmansoft.teach.Repository.ViewRepository.DailyActivity.StudentDailyActivityRepository;
import org.fatmansoft.teach.Repository.ViewRepository.HonorInfo.StudentHonorRepository;
import org.fatmansoft.teach.Repository.ViewRepository.PracticeInfo.StudentPracticeRepository;
import org.fatmansoft.teach.Repository.ViewRepository.StudentCampusCardCostRepository;
import org.fatmansoft.teach.Repository.ViewRepository.StudentCourseRepository;
import org.fatmansoft.teach.Repository.ViewRepository.StudentLeavingRepository;
import org.fatmansoft.teach.Repository.ViewRepository.StudentRepository;
import org.fatmansoft.teach.entity.ViewEntity.DailyActivity.StudentDailyActivity;
import org.fatmansoft.teach.entity.ViewEntity.HonorInfo.StudentHonor;
import org.fatmansoft.teach.entity.ViewEntity.PracticeInfo.StudentPractice;
import org.fatmansoft.teach.entity.ViewEntity.Student;
import org.fatmansoft.teach.entity.ViewEntity.StudentCampusCardCost;
import org.fatmansoft.teach.entity.ViewEntity.StudentCourse;
import org.fatmansoft.teach.entity.ViewEntity.StudentLeavingInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class GetStudentFullInfo {
    @Autowired
    StudentRepository studentRepository;

    @Autowired
    StudentPracticeRepository studentPracticeRepository;

    @Autowired
    StudentHonorRepository studentHonorRepository;

    @Autowired
    StudentDailyActivityRepository studentDailyActivityRepository;

    @Autowired
    StudentLeavingRepository studentLeavingRepository;

    @Autowired
    StudentCourseRepository studentCourseRepository;

    @Autowired
    StudentCampusCardCostRepository studentCampusCardCostRepository;

    public Map<String, Object> getStudentFullInfo(Integer studentId) {
//        从所有表中查询学生ID对应的数据
        Map<String, Object> data = new HashMap();

        Student student = studentRepository.findById(studentId).get();
        data.put("studentId", student.getId());   // 学生学号
        data.put("studentName", student.getStudentName());   // 学生姓名

        List<Map> fullInfoList = new ArrayList();
        Map map = new HashMap();
        List<String> jsonStr = new ArrayList<>();

//        添加学生基本信息(单元组)
        map = setTitle(map, "学生基本信息");  //设置题目
        jsonStr.add(setInfo(
                "性别:", student.getSex().toString(),
                "年龄:", student.getAge().toString(),
                "住址:", student.getAddress().toString(),
                "生日:", student.getBirthday().toString(),
                "手机号:", student.getCellPhone().toString()
        ));  //设置传入的字符串
        map.put("content", jsonStr);  //设置内容
        fullInfoList.add(map);  //加入返回的list

        //        添加学生选课信息（关系）
        map = setTitle(map, "学生选课信息");  //设置题目
        jsonStr = new ArrayList<>();  //初始化
        List<StudentCourse> studentCourses = studentCourseRepository.findAllByStudentId(studentId);  //从数据库中查找
        if (studentCourses.size() == 0) {  //没有信息被查出
            jsonStr.add(setInfo(
                    "无学生选课信息", ""));
        } else {
            for (StudentCourse studentCourse : studentCourses) {
                //需要对成绩进行判断后再计入
                String usualScore = "";
                if(studentCourse.getUsualScore() == null) usualScore = "教师尚未录入平时成绩";
                else usualScore = studentCourse.getUsualScore().toString();
                String finalScore = "";
                if(studentCourse.getFinalScore() == null) finalScore = "教师尚未录入期末成绩";
                else finalScore = studentCourse.getFinalScore().toString();
                String totalScore = "";
                if(studentCourse.getTotalScore() == null) totalScore = "学生暂未获得总成绩";
                else totalScore = studentCourse.getTotalScore().toString();
//                key后面加“:”！！
                jsonStr.add(setInfo(
                        "课程序号:", studentCourse.getId().toString(),
                        "课程名称:", getCourseName(studentCourse.getCourse().getId()),
                        "平时成绩:", usualScore,
                        "期末成绩:", finalScore,
                        "总成绩:", totalScore,
                        "学分绩点:",String.valueOf(new DecimalFormat("#0.0").format(calculation(studentId)))
                ));
            }
        }
        map.put("content", jsonStr);
        fullInfoList.add(map);

        //        添加学生荣誉信息（关系）
        map = setTitle(map, "学生荣誉信息");  //设置题目
        jsonStr = new ArrayList<>();  //初始化
        List<StudentHonor> studentHonors = studentHonorRepository.findAllByStudentId(studentId);  //从数据库中查找
        if (studentHonors.size() == 0) {  //没有信息被查出
            jsonStr.add(setInfo(
                    "无学生荣誉信息", ""));
        } else {
            for (StudentHonor studentHonor : studentHonors) {
//                key后面加“:”！！
                jsonStr.add(setInfo(
                        "比赛名称:", studentHonor.getGameName().toString(),
                        "获奖日期:", studentHonor.getPrizeDate().toString(),
                        "荣誉类型:", getHonorType(studentHonor.getHonorType().getId()),
                        "获奖名称:", studentHonor.getPrizeName().toString()
                ));
            }
        }
        map.put("content", jsonStr);
        fullInfoList.add(map);

//        添加学生实践信息（关系）
        map = setTitle(map, "学生实践信息");  //设置题目
        jsonStr = new ArrayList<>();  //初始化
        List<StudentPractice> studentPractices = studentPracticeRepository.findAllByStudentId(studentId);  //从数据库中查找
        if (studentPractices.size() == 0) {  //没有信息被查出
            jsonStr.add(setInfo(
                    "无学生实践信息", ""));
        } else {
            for (StudentPractice studentPractice : studentPractices) {
//                key后面加“:”！！
                jsonStr.add(setInfo(
                        "实践名称:", studentPractice.getPracticeName().toString(),
                        "实践类型:", getPracticeType(studentPractice.getPracticeType().getId()),
                        "实践开始日期:", studentPractice.getPracticeBeginDate().toString(),
                        "实践结束日期:", studentPractice.getPracticeEndDate().toString(),
                        "实践描述:", studentPractice.getPracticeDescription().toString()));
            }
        }
        map.put("content", jsonStr);
        fullInfoList.add(map);

        //        添加学生日常活动信息（关系）
        map = setTitle(map, "学生日常活动信息");  //设置题目
        jsonStr = new ArrayList<>();  //初始化
        List<StudentDailyActivity> studentDailyActivities = studentDailyActivityRepository.findAllByStudentId(studentId);  //从数据库中查找
        if (studentDailyActivities.size() == 0) {  //没有信息被查出
            jsonStr.add(setInfo(
                    "无学生日常活动信息", ""));
        } else {
            for (StudentDailyActivity studentDailyActivity : studentDailyActivities) {
//                key后面加“:”！！
                jsonStr.add(setInfo(
                        "活动类型:", getDailyActivityType((studentDailyActivity.getDailyActivityType().getId())),
                        "活动名称:", studentDailyActivity.getActivityName().toString(),
                        "活动日期:", studentDailyActivity.getActivityDate().toString(),
                        "活动描述:", studentDailyActivity.getActivityDescription().toString()));
            }
        }
        map.put("content", jsonStr);
        fullInfoList.add(map);

        //        添加学生外出信息（关系）
        map = setTitle(map, "学生外出信息");  //设置题目
        jsonStr = new ArrayList<>();  //初始化
        List<StudentLeavingInfo> studentLeavingInfoList = studentLeavingRepository.findAllByStudentId(studentId);  //从数据库中查找
        if (studentLeavingInfoList.size() == 0) {  //没有信息被查出
            jsonStr.add(setInfo(
                    "无学生外出信息", ""));
        } else {
            for (StudentLeavingInfo studentLeavingInfo : studentLeavingInfoList) {
//                key后面加“:”！！
                jsonStr.add(setInfo(
                        "外出地点:", studentLeavingInfo.getOutingLocation().toString(),
                        "外出时间:", studentLeavingInfo.getOutingTime().toString(),
                        "到校时间:", studentLeavingInfo.getComingTime().toString()
                ));
            }
        }
        map.put("content", jsonStr);
        fullInfoList.add(map);

        //        添加学生校园卡信息（关系）
        map = setTitle(map, "学生校园卡信息");  //设置题目
        jsonStr = new ArrayList<>();  //初始化
        List<StudentCampusCardCost> studentCampusCardCosts = studentCampusCardCostRepository.findAllByStudentId(studentId);  //从数据库中查找
        if (studentCampusCardCosts.size() == 0) {  //没有信息被查出
            jsonStr.add(setInfo(
                    "无学生校园卡信息", ""));
        } else {
            for (StudentCampusCardCost studentCampusCardCost : studentCampusCardCosts) {
//                key后面加“:”！！
                jsonStr.add(setInfo(
                        "类型:", getCostType(studentCampusCardCost.getCostType()),
                        "时间:", studentCampusCardCost.getCostTime().toString(),
                        "额度:", studentCampusCardCost.getCostInfo().toString()
                ));
            }
        }
        map.put("content", jsonStr);
        fullInfoList.add(map);

//        返回数据
        data.put("fullInfoList", fullInfoList);
        return data;
    }



    public String getPracticeType(Integer practiceTypeId) {
        if (practiceTypeId == 1) {
            return "社会实践";
        } else if (practiceTypeId == 2) {
            return "学科竞赛";
        } else if (practiceTypeId == 3) {
            return "科技成果";
        } else if (practiceTypeId == 4) {
            return "培训讲座";
        } else if (practiceTypeId == 5) {
            return "创新项目";
        } else {
            return "校外实习";
        }
    }

    public String getHonorType(Integer honorTypeId) {
        if (honorTypeId == 1) {
            return "院级";
        } else if (honorTypeId == 2) {
            return "校级";
        } else if (honorTypeId == 3) {
            return "省级";
        } else{
            return "国家级";
        }
    }

    public String getDailyActivityType(Integer activityTypeId) {
        if (activityTypeId == 1) {
            return "体育活动";
        } else if (activityTypeId == 2) {
            return "外出旅游";
        } else if (activityTypeId == 3) {
            return "文艺演出";
        } else{
            return "聚会";
        }
    }

    public String getCourseName(Integer courseId) {
        if (courseId == 1) {
            return "离散数学";
        } else if (courseId == 2) {
            return "线性代数";
        } else if (courseId == 3) {
            return "高数1";
        } else{
            return "算法";
        }
    }

    public String getCostType(Integer costTypeId) {
        if (costTypeId == 1) {
            return "充值";
        }
        else{
            return "消费";
        }
    }
    public double calculation(Integer sId){
        double gpa;
        double total=0;
        double credits=0;
        List<StudentCourse>studentCourses=studentCourseRepository.findAllByStudentId(sId);
        for(StudentCourse studentCourse:studentCourses){
            total=total+studentCourse.getCourse().getCredit()*studentCourse.getGpa();
            credits=credits+studentCourse.getCourse().getCredit();
        }
        gpa=total/credits;
        return gpa;
    }
    public Map<String, Object> setTitle(Map map, String title) {
        map = new HashMap();
        map.put("title", title);
        return map;
    }

    public String setInfo(String... list) {
        JSONObject json = new JSONObject(true);

        for (int i = 0; i < list.length; i++) {
            json.put(list[i], list[++i]);
        }

        return json.toString(); //获取到的信息转化为JSON
    }
}
